package com.hydl.power.controller;

import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.pojo.*;
import com.hydl.power.service.SysUserService;
import com.hydl.power.service.TransformerService;
import com.hydl.power.utils.Resp;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/transformer")
@Api(value = "TransformerController", tags = "变压器管理")
@CrossOrigin
public class TransformerController {
    @Autowired
    private TransformerService transformerService;
    @Autowired
    private SysUserService sysUserService;


    //  @LogAnnotation(module = "变压器管理",description = "根据条件查询变压器")
    @ApiOperation("根据条件查询变压器")
    @PostMapping("/selectByCondition")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数",  dataType = "Integer",defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数", dataType = "Integer",defaultValue = "10"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "城市id", dataType = "String",defaultValue = "0371"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "String",defaultValue = "50642475"),
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房id", dataType = "String",defaultValue = "70980664"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String",defaultValue = ""),
    })
    public Resp<PageInfo<Transformer>> selectByCondition(@RequestParam(required = false )Integer pageNum,
                                                         @RequestParam(required = false )Integer pageSize,
                                                         @RequestParam(required = false) String cityId,
                                                         @RequestParam(required = false) String projectId,
                                                         @RequestParam(required = false) String roomId,
                                                         @RequestParam(required = false) String token) {
        SysUser sysUser = sysUserService.getUserByToken(token);
        if(sysUser.getStatus() == 2 ){
            PageInfo<Transformer> list=transformerService.selectByToken(pageNum,pageSize,cityId,projectId,roomId,token);
            return Resp.success(list);
        }
        PageInfo<Transformer> list = transformerService.selectByCondition(pageNum, pageSize, cityId, projectId, roomId);
        return Resp.success(list);
    }


    @LogAnnotation(module = "变压器管理",description = "添加变压器")
    @ApiOperation("添加变压器")
    @PostMapping("/addTransformer")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电室id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "transformerName", value = "变压器名字", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "城市id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "deviceId", value = "设备id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "transformerCapacity", value = "变压器容量", dataType = "String"),
    })
    public Resp addTransformer(String roomId,String transformerName,String projectId ,String cityId ,String deviceId,String transformerCapacity) {
        if(StringUtils.isBlank(roomId) || StringUtils.isBlank(transformerName) || StringUtils.isBlank(projectId) || StringUtils.isBlank(cityId) || StringUtils.isBlank(deviceId)
          || StringUtils.isBlank(transformerCapacity)){
            return Resp.fail("参数不能为空");
        }
        return Resp.success(transformerService.addTransformer(roomId, transformerName, projectId, cityId,deviceId,transformerCapacity));
    }


    @LogAnnotation(module = "变压器管理",description = "删除变压器")
    @ApiOperation("删除变压器")
    @PostMapping("/deleteById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "Integer"),
    })
    public Resp deleteById(Integer id ) {
        int flag = transformerService.deleteById(id);
        if (flag == 1){
            return Resp.success("Success");
        }else{
            return Resp.fail("删除失败");
        }
    }


    @LogAnnotation(module = "变压器管理",description = "修改变压器")
    @ApiOperation("修改变压器")
    @PostMapping("/updateTrans")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "transformerName", value = "变压器名称", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "Integer"),
    })
    public Resp updateTrans(String transformerName,Integer id ) {
        int flag = transformerService.updateTrans(transformerName,id);
        if (flag == 1){
            return Resp.success("Success");
        }else{
            return Resp.fail("修改失败");
        }
    }


    // @LogAnnotation(module = "变压器管理",description = "根据id查询变压器")
    @ApiOperation("根据id查询变压器")
    @PostMapping("/selectById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "Integer"),
    })
    public Resp selectById(Integer id ) {

            return Resp.success(transformerService.selectById(id));
    }
}
